
#include "fast_gelu_grad_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"

namespace optiling {
const uint32_t BLOCK_SIZE = 32;
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
  FastGeluGradTilingData tiling;
  uint32_t sizeofdatatype;
  uint32_t totalLengthAligned;
  auto ascendcPlatform =
      platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
  auto socVersion = ascendcPlatform.GetSocVersion();
  uint64_t ub_size;
  ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ub_size);
  auto aivNum = ascendcPlatform.GetCoreNumAiv();
  auto aicNum = ascendcPlatform.GetCoreNumAic();

  uint32_t totalLength = context->GetInputTensor(0)->GetShapeSize();
  auto dt = context->GetInputTensor(0)->GetDataType();
  if (dt == 1) {
    sizeofdatatype = 2;
  }

  uint32_t ALIGN_NUM = BLOCK_SIZE / sizeofdatatype;
  uint32_t ub_block_num_real =
      ((ub_size) / BLOCK_SIZE / 2) * 13 /
      20;  
  uint32_t ub_block_num = 512;  
  uint32_t tile_num;

  if (ub_block_num % 2 != 0) {
    ub_block_num = ub_block_num - 1;
  }


  if (totalLength % ALIGN_NUM != 0) {  
    totalLengthAligned =
        ((totalLength + ALIGN_NUM - 1) / ALIGN_NUM) * ALIGN_NUM;
  } else {
    totalLengthAligned = totalLength;
  }

  if (totalLengthAligned <= ub_block_num * ALIGN_NUM) {  
    context->SetBlockDim(1);
  } else {
    if (((totalLengthAligned / ALIGN_NUM) % ub_block_num) ==
        0) { 
      if ((totalLengthAligned / ALIGN_NUM / ub_block_num) <=
          aivNum) {  
        context->SetBlockDim(totalLengthAligned / ALIGN_NUM / ub_block_num);

      } else {
        context->SetBlockDim(aivNum);
      }
    } else {  
      if (((totalLengthAligned / ALIGN_NUM / ub_block_num) + 1) <=
          aivNum) {  
        context->SetBlockDim((totalLengthAligned / ALIGN_NUM / ub_block_num) +
                             1);
      } else {
        context->SetBlockDim(aivNum);
      }
    }
  }

  auto block_dim = context->GetBlockDim();
  uint32_t blockLength = 0;
  uint32_t tileLength = 0;
  uint32_t lasttileLength = 0;

  blockLength = totalLengthAligned / block_dim;
  tile_num = blockLength / ALIGN_NUM / ub_block_num;
  if ((totalLengthAligned / block_dim / ALIGN_NUM) % ub_block_num == 0 ||
      tile_num == 0) {  
    if (tile_num == 0) {
      tile_num = 1;
    } 
    if (blockLength < ub_block_num * ALIGN_NUM) {
      tileLength = ((blockLength / ALIGN_NUM) + 1) / 2 * 2 * ALIGN_NUM;
      lasttileLength = tileLength;
    } else {
      tileLength = ub_block_num * ALIGN_NUM;
      lasttileLength = tileLength;
    }
  } else {  
    tile_num = tile_num + 1;
    tileLength = ub_block_num * ALIGN_NUM;
    lasttileLength = blockLength - (tile_num - 1) * tileLength;
  }
  context->SetTilingKey(1);
  tiling.set_blockLength(blockLength);
  tiling.set_tileNum(tile_num);
  tiling.set_tileLength(tileLength);
  tiling.set_lasttileLength(lasttileLength);

  tiling.SaveToBuffer(context->GetRawTilingData()->GetData(),
                      context->GetRawTilingData()->GetCapacity());
  context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
  size_t* currentWorkspace = context->GetWorkspaceSizes(1);
  currentWorkspace[0] = 0;
  return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class FastGeluGrad : public OpDef {
public:
    explicit FastGeluGrad(const char* name) : OpDef(name)
    {
        this->Input("dy")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("z")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(FastGeluGrad);
}
